package com.qf.team2.netdisc.admin.service.impl;

import com.qf.team2.netdisc.admin.dao.AdminMapper;
import com.qf.team2.netdisc.admin.service.AdminManageService;
import com.qf.team2.netdisc.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理操作服务接口实现类
 *
 * @author Simon_Wong
 * @date 2021/05/21
 * @since 2021/5/20 19:56
 */
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
@Service
public class AdminManageServiceImpl implements AdminManageService {

    @Autowired
    private AdminMapper adminMapper;

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public int setVIPByUserId(Integer userId) {
        int result = adminMapper.setVIP(userId);
        if (result == 0) {
            throw new BusinessException("找不到userId:"+userId+"的用户");
        }
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        return result;
    }

    @Override
    public List<Integer> setAllUserVIP(Integer... userIds) {
        List<Integer> failedList = new ArrayList<>();
        for (Integer userId : userIds) {
            try {
                setVIPByUserId(userId);
            } catch (BusinessException e) {
                e.printStackTrace();
                failedList.add(userId);
            }
        }
        return failedList;
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public int setDocumentDeleted(Integer documentId) {
        int result = adminMapper.setDocumentDeleted(documentId);
        if (result == 0) {
            throw new BusinessException("找不到documentId:"+documentId+"的文件");
        }
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        return result;
    }

    @Override
    public List<Integer> setAllDocumentDeleted(Integer... documentIds) {
        List<Integer> failedList = new ArrayList<>();
        for (Integer documentId : documentIds) {
            try {
                setDocumentDeleted(documentId);
            } catch (BusinessException e) {
                e.printStackTrace();
                failedList.add(documentId);
            }
        }
        return failedList;
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public int setCommonByUserId(Integer userId) {
        int result = adminMapper.setCommon(userId);
        if (result == 0) {
            throw new BusinessException("找不到userId:"+userId+"的用户");
        }
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        return result;
    }

    @Override
    public List<Integer> setAllUserCommon(Integer... userIds) {
        List<Integer> failedList = new ArrayList<>();
        for (Integer userId : userIds) {
            try {
                setCommonByUserId(userId);
            } catch (BusinessException e) {
                e.printStackTrace();
                failedList.add(userId);
            }
        }
        return failedList;
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public int setDocumentUndeleted(Integer documentId) {
        int result = adminMapper.setDocumentUndeleted(documentId);
        if (result == 0) {
            throw new BusinessException("找不到documentId:"+documentId+"的文件");
        }
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        return result;
    }


    @Override
    public List<Integer> setAllDocumentUndeleted(Integer... documentIds) {
        List<Integer> failedList = new ArrayList<>();
        for (Integer documentId : documentIds) {
            try {
                setDocumentUndeleted(documentId);
            } catch (BusinessException e) {
                e.printStackTrace();
                failedList.add(documentId);
            }
        }
        return failedList;
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public int setUserDeleted(Integer userId) {
        int result = adminMapper.setUserDeleted(userId);
        if (result == 0) {
            throw new BusinessException("找不到userId:"+userId+"的用户");
        }
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        return result;
    }

    @Override
    public List<Integer> setAllUserDeleted(Integer... userIds) {
        List<Integer> failedList = new ArrayList<>();
        for (Integer userId : userIds) {
            try {
                setUserDeleted(userId);
            } catch (BusinessException e) {
                e.printStackTrace();
                failedList.add(userId);
            }
        }
        return failedList;
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public int setUserUndeleted(Integer userId) {
        int result = adminMapper.setUserUndeleted(userId);
        if (result == 0) {
            throw new BusinessException("找不到userId:"+userId+"的用户");
        }
        if (result != 1) {
            throw new BusinessException("未知错误");
        }
        return result;
    }

    @Override
    public List<Integer> setAllUserUndeleted(Integer... userIds) {
        List<Integer> failedList = new ArrayList<>();
        for (Integer userId : userIds) {
            try {
                setUserUndeleted(userId);
            } catch (BusinessException e) {
                e.printStackTrace();
                failedList.add(userId);
            }
        }
        return failedList;
    }
}
